package com.zjhome.cloudnote.controller;


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zjhome.cloudnote.constant.ErrorCode;
import com.zjhome.cloudnote.domain.UserInfo;
import com.zjhome.cloudnote.exception.BaseException;
import com.zjhome.cloudnote.param.LoginParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 注册接口
 * 
 * @author 0283000121
 *
 */
//@Controller
@RestController
@RequestMapping("/users")
@Api(value = "注册管理", description = "注册管理")
public class RegisterController extends BaseController {
	
	@ApiOperation(value = "注册用户")
	@PostMapping("/signup")
//	@ResponseBody
	public UserInfo signup(LoginParam loginParam) throws Exception {
		UserInfo findUser = userService.selectUserById(loginParam.getUsername());
		if (findUser != null) {
//			throw new UsernameIsExitedException("用户已经存在");
			throw new BaseException("", ErrorCode.ERR_REG_USER_EXISTS);
		}
		
		UserInfo newUser = new UserInfo();
		
		newUser.setId(loginParam.getUsername());
		newUser.setNickname(loginParam.getUsername());	// 昵称先和用户名一致		
		newUser.setPassword(bCryptPasswordEncoder.encode(loginParam.getPassword()));
		userService.insertUserByParam(newUser);
		return newUser;
	}
	
}

